#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@File: insertClient_wyf_20250213.py
@Author: lijk34925 
@Date: 2025/2/13 21:07
@desc: 请补充本模块功能说明
"""

import threading
import queue
from queue import Empty
from DataTest.databases import MysqlUtil, DaMengUtil, OracleUtil
import datetime

from faker import Faker

from random import choice

from DataTest.sqlmontage.yanfeng_test import montage_sql
from DataTest.tabledata.commonData import randomInfo
from DataTest.tabledata.commonData.SQLMontage import read_file, get_sql_key, montage_sql_value
from DataTest.tabledata.commonData.clientInfo import client_info

fake = Faker(locale='zh_CN')

# 定义全局变量num_threads线程数量，num_inserts插入数据总量，batch_size单次插入数量
from DataTest.tabledata.commonData.SQLMontage import get_sql_key, get_sql, montage_sql_value

num_threads = 10
# 插入数量= num_threads * sql_number
insert_count = 200
sql_number = 50

processes = []

# 定义队列用于存储拼接好的SQL语句
sql_queue = queue.Queue()

# 定义锁，用于保证线程安全
lock = threading.Lock()

BLOCK = True
customerinfo_key = get_sql_key(table='fnd_client')
customerinfo_value = get_sql(table='fnd_client')
acco_key = get_sql_key(table='fnd_fund_account')
acco_value = get_sql(table='fnd_fund_account')


# 定义线程执行的函数：SQL拼接
def sql_assemble(thread_id):
    index_number = (thread_id + 1) * 10 * 10000
    for i in range(insert_count):
        customerinfo_sql = customerinfo_key
        acco_sql = acco_key
        for j in range(0, sql_number):
            index_number += 1
            client_id = f"249760{index_number}"
            sql_list = client_info()
            ta_no = '63'
            id_type = choice(['01', '02', '15', '05', '06', '07'])
            sql_list.update({'client_id': client_id, 'inner_client_id': client_id, 'client_type': 0, 'fund_account': f'HS{client_id}',
                             'ta_no': ta_no, 'id_type': id_type})
            # montage_sql(table='fnd_client', sql_list=sql_list)
            # montage_sql(table='fnd_fund_account', sql_list=sql_list)

            customerinfo_sql = customerinfo_sql + montage_sql_value(table=customerinfo_value, sql_list=sql_list)
            acco_sql = acco_sql + montage_sql_value(table=acco_value, sql_list=sql_list)

            if j < sql_number - 1:
                customerinfo_sql = customerinfo_sql + ','
                acco_sql = acco_sql + ','
            else:
                customerinfo_sql = customerinfo_sql + ';'
                acco_sql = acco_sql + ';'

                sql_queue.put(customerinfo_sql)
                sql_queue.put(acco_sql)


# 定义线程执行的函数：SQL插入
def sql_insert(db):
    # while not sql_queue.empty():
    global sql
    while True:
        lock.acquire(blocking=True, timeout=-1)
        try:
            # print(sql_queue.qsize())
            sql = sql_queue.get(block=BLOCK)
            # print(f'插入sql:{sql}\n')
            # 执行SQL插入操作
        except Empty as e:
            break
        finally:
            lock.release()
            if sql:
                print(sql)
                db.execsql(sql)
                print(datetime.datetime.now())

            else:
                break


if __name__ == '__main__':
    print(datetime.datetime.now())
    # 创建并启动SQL拼接线程
    assemble_threads = []
    for i in range(num_threads):
        thread = threading.Thread(target=sql_assemble, args=(i,))
        thread.start()
        assemble_threads.append(thread)

    # 创建并启动SQL插入线程
    insert_threads = []
    db = MysqlUtil('root', 'DFLTdf87220bf0', '10.20.181.116', port=33061)
    # db = MysqlUtil('root', 'fxq5@123', '10.20.46.217', port=33061)
    # db = OracleUtil('hsraw', 'fxq5@123', '10.20.37.111', server='bsaml')
    for i in range(num_threads):
        thread = threading.Thread(target=sql_insert, args=(db,))
        thread.start()
        insert_threads.append(thread)

    # 等待所有SQL拼接线程执行完毕
    for thread in assemble_threads:
        thread.join()
    else:
        BLOCK = False
        sql_queue.put(False)

    # 等待所有SQL插入线程执行完毕
    # for thread in insert_threads:
    #     thread.join()
    db.commit()
    print(datetime.datetime.now())